﻿
// MFCReadDocView.cpp: CMFCReadDocView 类的实现
//

#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义，并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "MFCReadDoc.h"
#endif

#include "MFCReadDocDoc.h"
#include "MFCReadDocView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMFCReadDocView

IMPLEMENT_DYNCREATE(CMFCReadDocView, CView)

BEGIN_MESSAGE_MAP(CMFCReadDocView, CView)
	// 标准打印命令
	ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CMFCReadDocView::OnFilePrintPreview)
	ON_WM_CONTEXTMENU()
	ON_WM_RBUTTONUP()
	ON_WM_LBUTTONDOWN()
	ON_UPDATE_COMMAND_UI(ID_FILE_READ, &CMFCReadDocView::OnUpdateFileRead)
	ON_UPDATE_COMMAND_UI(ID_FILE_WRITE, &CMFCReadDocView::OnUpdateFileWrite)
	ON_COMMAND(ID_FILE_READ, &CMFCReadDocView::OnFileRead)
	ON_COMMAND(ID_FILE_WRITE, &CMFCReadDocView::OnFileWrite)
	ON_WM_RBUTTONDOWN()
	ON_COMMAND(ID_SaveToFile, &CMFCReadDocView::OnSavetofile)
END_MESSAGE_MAP()

// CMFCReadDocView 构造/析构

CMFCReadDocView::CMFCReadDocView() noexcept
{
	// TODO: 在此处添加构造代码

}

CMFCReadDocView::~CMFCReadDocView()
{
}

BOOL CMFCReadDocView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CView::PreCreateWindow(cs);
}

// CMFCReadDocView 绘图

void CMFCReadDocView::OnDraw(CDC* pDC)
{
	CMFCReadDocDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	CPen newPen;
	CPen* pOldPen;
	//创建画笔
	newPen.CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
	//选择画笔
	pOldPen = pDC->SelectObject(&newPen);

	// TODO: 在此处为本机数据添加绘制代码
	pDC->Ellipse(pDoc->startPoint.x, pDoc->startPoint.y,
		pDoc->endPoint.x, pDoc->endPoint.y
	);
	pDC->SelectObject(pOldPen);

	pDC->TextOutW(10, 10, L"我爱你，爱因斯坦，你是一个非常伟大的科学家");
	
}


// CMFCReadDocView 打印


void CMFCReadDocView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
	AFXPrintPreview(this);
#endif
}

BOOL CMFCReadDocView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// 默认准备
	return DoPreparePrinting(pInfo);
}

void CMFCReadDocView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加额外的打印前进行的初始化过程
}

void CMFCReadDocView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: 添加打印后进行的清理过程
}

void CMFCReadDocView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
	ClientToScreen(&point);
	OnContextMenu(this, point);
}

void CMFCReadDocView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
	theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}


// CMFCReadDocView 诊断

#ifdef _DEBUG
void CMFCReadDocView::AssertValid() const
{
	CView::AssertValid();
}

void CMFCReadDocView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CMFCReadDocDoc* CMFCReadDocView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMFCReadDocDoc)));
	return (CMFCReadDocDoc*)m_pDocument;
}
#endif //_DEBUG


// CMFCReadDocView 消息处理程序


void CMFCReadDocView::OnLButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	CView::OnLButtonDown(nFlags, point);
	

	MessageBox(TEXT("我是中国人，我爱国"), TEXT("愚人节"), MB_OK);
}


void CMFCReadDocView::OnUpdateFileRead(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	//pCmdUI->Enable(true);
}


void CMFCReadDocView::OnUpdateFileWrite(CCmdUI* pCmdUI)
{
	// TODO: 在此添加命令更新用户界面处理程序代码
	//pCmdUI->Enable(true);
}


void CMFCReadDocView::OnFileRead()
{
	//MessageBox(L"读文件", L"愚人节", MB_OK);
	//2024.4.1: 读文件只能选择CFile::modeRead，而不能是CFile::modeRead| CFile::modeWrite
	try
	{
		CFile file(L"d:\\log\\1.txt", CFile::modeRead);
		CArchive ar(&file, CArchive::load);
		int startX = 0;
		int startY = 0;
		int endX = 0;
		int endY = 0;
		//将数据读取
		ar >> startX >>  startY >> endX >> endY;
		ar.Close();
		file.Close();


		CString str;
		str.Format(_T("坐标信息是 startX=%d, startY=%d, endX=%d, endY=%d"), startX, startY,endX,endY);
		//MessageBox(NULL, str, MB_OK);
		MessageBox(str, L"哈哈", MB_OK);
	}
	catch (CFileException* e)
	{
		if (e->m_cause == CFileException::fileNotFound) {
			TRACE(_T("ERROR: File not found\n"));
		}
		e->Delete();
	}

}


void CMFCReadDocView::OnFileWrite()
{
	try
	{
		//2024.4.1: 写入文件；如果文件不存在，则创建文件
		CFile file(L"d:\\log\\1.txt", CFile::modeCreate | CFile::modeWrite);
		CArchive ar(&file, CArchive::store);
		int startX = 50;
		int startY = 50;
		int endX = 100;
		int endY = 100;
		//将数据写入
		ar << startX << startY << endX << endY;
		ar.Close();
		file.Close();


	}
	catch (CFileException* e)
	{
		if (e->m_cause == CFileException::fileNotFound) {
			TRACE(_T("ERROR: File not found\n"));
		}
		e->Delete();
		
	}
}

//2024.4.1: 右键，将当前点击的点保存到文件中
void CMFCReadDocView::OnRButtonDown(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值

	this->activePoint = point;
	CView::OnRButtonDown(nFlags, point);
	
}

//2024.4.1: 右键，将当前点击的点保存到文件中
void CMFCReadDocView::OnSavetofile()
{
	// TODO: 在此添加命令处理程序代码
	try
	{
		CFile file;
		//2024.4.1: 写入文件；如果文件不存在，则创建文件
		file.Open(L"d:\\log\\1.txt", CFile::modeCreate | CFile::modeWrite);
		CArchive ar(&file, CArchive::store);

		//将数据写入
		ar << this->activePoint.x << this->activePoint.y << this->activePoint.x + 100 << this->activePoint.y + 100;
		ar.Close();
		file.Close();
		CDC* cdc = this->GetDC();
		CString msg = L"写入文件成功";
		TextOut(*cdc, 0, 0, msg, msg.GetLength());


	}
	catch (CFileException* e)
	{
		if (e->m_cause == CFileException::fileNotFound) {
			TRACE(_T("ERROR: File not found\n"));
		}
		e->Delete();
	}
}
